// prisma/seed.ts
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  console.log('开始创建初始数据...')

  // 创建分类
  const categories = await Promise.all([
    prisma.category.upsert({
      where: { slug: 'tech' },
      update: {},
      create: {
        name: 'tech',
        slug: 'tech',
        displayName: '技术分享'
      }
    }),
    prisma.category.upsert({
      where: { slug: 'wallpaper' },
      update: {},
      create: {
        name: 'wallpaper',
        slug: 'wallpaper',
        displayName: '壁纸分享'
      }
    }),
    prisma.category.upsert({
      where: { slug: 'article' },
      update: {},
      create: {
        name: 'article',
        slug: 'article',
        displayName: '文章分享'
      }
    }),
    prisma.category.upsert({
      where: { slug: 'game' },
      update: {},
      create: {
        name: 'game',
        slug: 'game',
        displayName: '游戏分享'
      }
    }),
    prisma.category.upsert({
      where: { slug: 'video' },
      update: {},
      create: {
        name: 'video',
        slug: 'video',
        displayName: '视频分享'
      }
    })
  ])

  console.log('分类创建完成:', categories.map(c => c.displayName))

  // 创建默认用户
  const user = await prisma.user.upsert({
    where: { email: 'admin@example.com' },
    update: {},
    create: {
      username: 'admin',
      email: 'admin@example.com',
      password: 'temp_password',
      role: 'ADMIN'
    }
  })

  console.log('默认用户创建完成:', user.username)

  // 创建示例技术文章
  const techContent = await prisma.content.create({
    data: {
      title: '欢迎来到我的个人网站',
      description: '这是我的第一个技术文章示例',
      slug: 'welcome-to-my-site',
      coverImage: null,
      categoryId: categories.find(c => c.slug === 'tech')!.id,
      authorId: user.id,
      isPublished: true,
      isFeatured: true,
      techArticle: {
        create: {
          content: '# 欢迎\n\n这是我的个人网站，分享技术、壁纸、文章、游戏和视频等内容。\n\n## 功能特点\n\n- 技术分享\n- 壁纸下载\n- 文章阅读\n- 游戏评测\n- 视频推荐',
          toc: '["欢迎", "功能特点"]'
        }
      }
    },
    include: {
      techArticle: true
    }
  })

  console.log('技术文章创建完成:', techContent.title)

  // 创建示例壁纸
  const wallpaperContent = await prisma.content.create({
    data: {
      title: '美丽的风景壁纸',
      description: '一组高质量的自然风景壁纸',
      slug: 'beautiful-landscape-wallpapers',
      coverImage: '/images/wallpaper1.jpg',
      categoryId: categories.find(c => c.slug === 'wallpaper')!.id,
      authorId: user.id,
      isPublished: true,
      isFeatured: false,
      wallpaper: {
        create: {
          imageUrls: '["/images/wallpaper1.jpg", "/images/wallpaper2.jpg"]',
          downloadLink: '/download/wallpapers.zip',
          resolution: '1920x1080',
          fileSize: '15.2MB'
        }
      }
    },
    include: {
      wallpaper: true
    }
  })

  console.log('壁纸内容创建完成:', wallpaperContent.title)

  // 创建示例游戏
  const gameContent = await prisma.content.create({
    data: {
      title: '艾尔登法环游戏评测',
      description: 'FromSoftware 的又一力作',
      slug: 'elden-ring-review',
      coverImage: '/images/elden-ring.jpg',
      categoryId: categories.find(c => c.slug === 'game')!.id,
      authorId: user.id,
      isPublished: true,
      isFeatured: true,
      game: {
        create: {
          platform: 'PC, PlayStation, Xbox',
          genre: '动作角色扮演',
          score: 9.5,
          officialWebsite: 'https://www.eldenring.com',
          steamLink: 'https://store.steampowered.com/app/1245620'
        }
      }
    },
    include: {
      game: true
    }
  })

  console.log('游戏内容创建完成:', gameContent.title)
}

main()
  .catch((e) => {
    console.error('种子数据创建失败:', e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })